/**
 * related to userDetail.ui
 * 
 * @Author : wmn1988@qq.com
 * @Timestamp : 2016-07-06
 */
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var root = ui("$");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var imgTool = require("helper/picture");
var urlUser = require("url/user");
var openHelper = require("helper/open");
var userUtil = require("helper/user");
var userId = null;

//andriod返回键
page.on("back", function() {
    app.closePage({"fromPage":"baseUserDetail"});
});

page.on("loaded", function(){
	loadUserDetail();
});

//从新增页面返回,刷新数据
page.on("result", function(data){
	if (data.user_update) {
		loadUserDetail();
    }
});

function loadUserDetail(){
	http.post(urlUser.url.base.detail, {}, function(data) {
		userId = data.userBase.id;
		ui(addNickName+".labelText").text = data.userBase.nick;
		ui(addPhone+".labelText").text = data.phone;
		ui(addInterests+".labelText").text = data.userBase.interests;
		ui(addIndustry+".labelText").text = getNameById(data.userBase.profession,industryDatas);
		ui(addHomeTown+".labelText").text = getNameById(data.userBase.hometown,hometownDatas);
		ui(addDelicacy+".labelText").text = data.userBase.delicacys;
		ui(addSchool+".labelText").text = data.userBase.graduateSchool;
		ui(addEntertainment+".labelText").text = data.userBase.entertainment;
		if(!data.userBase.logo){
			ui(addUserImg+".thumb").source = "source://image/common/userLogin.png"
		}else{
			ui(addUserImg+".thumb").source = data.userBase.fullLogo;
		}
		ui(addSex+".labelText").text = data.userBase.sexDesc;
		if (userUtil.hasIdentityAuth() == "1") {
			ui(addAuth+".labelText").text = "已认证";
			ui(addAuth).off("touch");
		} else {
			ui(addAuth+".labelText").text = "未认证";
		}
	});
};

var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage({"fromPage":"baseUserDetail"});
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "个人信息"
})
header.refreshData();

var linearLayout_main = ui("do_LinearLayout_main");

/** -------------------add UI start ---------------------------**/
var addUserImg = linearLayout_main.add("userImg", "source://view/user/base/userLogoLabel.ui",0,0);
var userImg = ui("userImg");
userImg.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "头像",
    "labelTipsVisible":false
});
userImg.refreshData();
//更换头像
userImg.on("touch", function() {
	imgTool.imgCut(selectImgBack);//选择图片并裁剪
});
function selectImgBack(imgList){
	http.form(imgList,"user/base",function(imgDatas){
		var string4imgs = imgDatas.path;//返回图片云端路径
		var logoData = {
			id:userId,
			logo:string4imgs
		}
		http.post(urlUser.url.base.doUpdate,logoData,function(data){
			userUtil.userLogo(data.fullLogo);
			app.fire("fireUserUpdate",{"logo":data.fullLogo,"nick":""});
			imHelper.reflashCacheMe();//更新我的信息缓存
			nf.toast("头像上传成功");
			loadUserDetail();
		});
	});
};
/*昵称*/
var addNickName = linearLayout_main.add("nickName", "source://view/reuse/form/textLabel.ui","userImg");
var nickName = ui("nickName");
nickName.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "昵称",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
nickName.refreshData();
//修改昵称
nickName.on("touch", function() {
	openHelper.open("source://view/user/base/nickInput.ui", {
		id : userId,
		content : ui(addNickName+".labelText").text
	});
});
/*性别*/
var addSex = linearLayout_main.add("sex", "source://view/reuse/form/textLabel.ui","nickName");
var sex = ui("sex");
sex.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "性别",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
sex.refreshData();
var sexLabelText = ui(addSex+".labelText");
var addPicker = root.add("picker","source://view/reuse/form/oneCategory.ui",0,0);
var picker = ui("picker");
//修改性别
sex.on("touch","",300,function(){
	var param={}
	var ptitle={"title":"选择性别","id":"sexId"}
	var datas = [{id:"1",name:"男"},{id:"2",name:"女"}];
	var pcount = [ptitle,datas];
	page.fire("oneCategoryList",pcount);
	page.fire("oneCategory","true");
});
function getNameById(id,arrayData)
{
  for(var i=0;i<arrayData.length;i++)
	  {
	  if(arrayData[i]["id"] == id)
		  return arrayData[i]['name'];	  
	  }
}
page.on("refreshOneCategory",function(data){
	if("sexId" == data[1]){
		var dataPost = {
				id:userId,
				sex:data[0].oneid  //1-男，2-女
		};
		sexLabelText.text = data[0].onename;
	}
	else if("industryId" == data[1])
		{
		var dataPost = {
				id:userId,
				profession:data[0].oneid   
		};
		industryLabelText.text = data[0].onename;
		}
	else if("homeTownId" == data[1])
		{
		var dataPost = {
				id:userId,
				hometown:data[0].oneid   
		};
		hometownLabelText.text = data[0].onename;
		}
	http.post(urlUser.url.base.doUpdate,dataPost,function(data){
	});
});
/*爱好*/
var addInterests = linearLayout_main.add("interests", "source://view/reuse/form/textLabel.ui","sex");
var interests = ui("interests");
interests.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "爱好",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
interests.refreshData();
//选择爱好后返回刷新
page.on("refreshInterest",function(data){
	ui(addInterests+".labelText").text = data.interests;
});
var selectInterestsUi = ui(root.add("selectIntests","source://view/user/base/selectInterests.ui",0,0));
interests.on("touch","",300,function(){
	var initSelect = ui(addInterests+".labelText").text
	page.hideKeyboard();
	page.fire("interests");
	page.fire("getInterests",{"id":userId,"initSelect":initSelect});
});
/*行业*/
var addIndustry = linearLayout_main.add("industry", "source://view/reuse/form/textLabel.ui","interests");
var industry = ui("industry");
industry.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "行业",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
industry.refreshData();
var industryLabelText = ui(addIndustry+".labelText");
var industryDatas = JSON.parse(userUtil.loadUserPara(1,'industry'));
for(var i=0;i<industryDatas.length;i++)
{
	industryDatas[i]['id'] = industryDatas[i]['val'];
}
industry.on("touch","",300,function(){
	var param={}
	var ptitle={"title":"选择行业","id":"industryId"}	
	var pcount = [ptitle,industryDatas];
	page.fire("oneCategoryList",pcount);
	page.fire("oneCategory","true");
});

/*故乡*/
var addHomeTown = linearLayout_main.add("hometown", "source://view/reuse/form/textLabel.ui","industry");
var hometown = ui("hometown");
hometown.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "故乡",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
hometown.refreshData();
var hometownLabelText = ui(addHomeTown+".labelText");
var hometownDatas = JSON.parse(userUtil.loadUserPara(1,'hometown'));
for(var i=0;i<hometownDatas.length;i++)
{
	hometownDatas[i]['id'] = hometownDatas[i]['val'];
}

hometown.on("touch","",300,function(){
	var param={}
	var ptitle={"title":"选择故乡","id":"homeTownId"}
	;
	var pcount = [ptitle,hometownDatas];
	page.fire("oneCategoryList",pcount);
	page.fire("oneCategory","true");
});


/*美食*/
var addDelicacy = linearLayout_main.add("delicacy", "source://view/reuse/form/textLabel.ui","hometown");
var delicacy = ui("delicacy");
delicacy.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "美食",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
delicacy.refreshData();
//选择爱好后返回刷新
page.on("refreshDelicacy",function(data){
	ui(addDelicacy+".labelText").text = data.delicacys;
});
var selectDelicacyUi = ui(root.add("selectDelicacy","source://view/user/base/selectDelicacy.ui",0,0));
delicacy.on("touch","",300,function(){
	var initSelect = ui(addDelicacy+".labelText").text
	page.hideKeyboard();
	page.fire("delicacy");
	page.fire("getDelicacy",{"id":userId,"initSelect":initSelect});
});
/*学校*/
var addSchool = linearLayout_main.add("graduateSchool", "source://view/reuse/form/textLabel.ui","delicacy");
var school = ui("graduateSchool");
school.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "毕业学校",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
school.refreshData();
/*娱乐*/
var addEntertainment = linearLayout_main.add("entertainment", "source://view/reuse/form/textLabel.ui","graduateSchool");
var entertainment = ui("entertainment");
entertainment.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "娱乐",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
entertainment.refreshData();
//选择娱乐后返回刷新
page.on("refreshEntertainment",function(data){
	ui(addEntertainment+".labelText").text = data.entertainment;
});
var selectEntertainmentUi = ui(root.add("selectEntertainment","source://view/user/base/selectEntertainment.ui",0,0));
entertainment.on("touch","",300,function(){
	var initSelect = ui(addEntertainment+".labelText").text
	page.hideKeyboard();
	page.fire("entertainment");
	page.fire("getEntertainment",{"id":userId,"initSelect":initSelect});
});

//修改学校
school.on("touch", function() {
	openHelper.open("source://view/user/base/schoolInput.ui", {
		id : userId,
		content : ui(addSchool+".labelText").text
	});
});
var addLine1 = linearLayout_main.add("line1", "source://view/reuse/form/line.ui","entertainment");
var line1 = ui("line1");
line1.refreshData();

var addPhone = linearLayout_main.add("phone", "source://view/reuse/form/textLabel.ui","line1");
var phone = ui("phone");
phone.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "绑定手机",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
phone.refreshData();
/*身份认证*/
var addAuth = linearLayout_main.add("auth", "source://view/reuse/form/textLabel.ui","phone");
var auth = ui("auth");
auth.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "身份认证",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
auth.refreshData();
ui(addAuth).on("touch", function(data, e) {
	openHelper.open("source://view/user/auth/edit.ui");
});
/*修改密码*/
var addUpdatePwd = linearLayout_main.add("updatePwd", "source://view/reuse/form/textLabel.ui","auth");
var updatePwd = ui("updatePwd");
updatePwd.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "修改密码",
    "labelText":'',
    "labelTipsVisible":true,
    "labelTips":"修改您的登录密码"
});
updatePwd.refreshData();
ui(addUpdatePwd).on("touch", function(data, e) {
	openHelper.open("source://view/user/changePwd.ui");
});


var addLine2 = linearLayout_main.add("line2", "source://view/reuse/form/line.ui","updatePwd");
var line2 = ui("line2");
line2.refreshData();
/*我的地址*/
var addAddress = linearLayout_main.add("address", "source://view/reuse/form/textLabel.ui","line2");
var address = ui("address");
address.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "我的地址",
    "labelText":'',
    "labelTipsVisible":true,
    "labelTips":"用于收货的地址"
});
address.refreshData();
address.on("touch", function(data, e) {
	openHelper.open("source://view/user/userAddress/list.ui");
});
/** -------------------add UI end ---------------------------**/

app.on("fireUserUpdate",function(d){
	if(d.logo&&d.logo!==""){
		ui(addUserImg+".thumb").source=d.logo;
	}
    if(d.nick&&d.nick!==""){
    	ui(addNickName+".labelText").text=d.nick;
	}
    if(d.school&&d.school!==""){
    	ui(addSchool+".labelText").text=d.school;
	}
});
